package cn.kys.modules.system.controller;

import cn.kys.logging.aop.log.EnableKysLog;
import cn.kys.modules.system.domain.vo.Result;
import cn.kys.modules.system.pojo.SubtaskGeom;
import cn.kys.modules.system.service.MobileService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/mobile_api")
@Api(tags = {"业务——手机端模块"})
public class MobileApi {

    @Autowired
    private MobileService mobileService;

    @ApiOperation("手机端——获得所有任务")
    @GetMapping("/getTasks")
    public Map getTasks() {
        Map returnMap = new HashMap();
        try {
            List list = mobileService.getTasks();
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/getTasksPC")
    public Map getTasksPC() {
        Map returnMap = new HashMap();
        try {
            List list = mobileService.getTasksPC();
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //id任务taskid
    @GetMapping("/getSubTasks")
    public Map getSubTasks(@RequestParam("taskid") String taskid, @RequestParam("caseid") String caseid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.getSubTasksPG(taskid, caseid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/getDetail")
    public Map getDetail(@RequestParam("edit_geoid") String edit_geoid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.getDetail(edit_geoid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    //提交表单,id为子任务的id,content为表单内容（json格式）
    @PostMapping("/submitTableContent")
    public Map submitTableContent(@RequestBody String tableContent) {
        JSONObject jsonObject = JSONObject.parseObject(tableContent);
        Map returnMap = new HashMap();
        try {
            String subtaskid = jsonObject.get("subtaskid").toString();
            String edit_geoid = jsonObject.get("edit_geoid").toString();
            String status = jsonObject.get("status").toString();
            String content = jsonObject.get("content").toString();
//            String signature = jsonObject.get("signature").toString();

            mobileService.submitTableContent(subtaskid, edit_geoid, status, content);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    //提交表单,id为子任务的id,uploadFile为图片
    @PostMapping("/submitTableFile")
    public Map submitTableFile(@RequestParam("subtaskid") String subtaskid, @RequestParam("edit_geoid") String edit_geoid, @RequestParam("formcontentname") String formcontentname, @RequestParam("uploadFile") MultipartFile uploadFile
            , @RequestParam("alpha") String alpha
            , @RequestParam("beta") String beta
            , @RequestParam("gamma") String gamma
            , @RequestParam("lat") String lat
            , @RequestParam("lon") String lon) {
        Map returnMap = new HashMap();
        try {
            String filename = mobileService.submitTableFile(subtaskid, edit_geoid, formcontentname, uploadFile, alpha, beta, gamma, lat, lon);
            returnMap.put("data", filename);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    /**
     * @return
     * @Author zhaoxw
     * @Description //TODO  更改照片朝向
     * @Date 13:35 2024/1/10
     **/
    @PostMapping("/updatePhotoInMongoDb")
    public Map submitTableFile(@RequestParam("filename") String filename
            , @RequestParam("alpha") String alpha
            , @RequestParam("beta") String beta
            , @RequestParam("gamma") String gamma
            , @RequestParam("lat") String lat
            , @RequestParam("lon") String lon) {
        Map returnMap = new HashMap();
        try {
            mobileService.updatePhotoInMongoDb(filename, alpha, beta, gamma, lat, lon);
            returnMap.put("data", "已更改图片朝向");
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    //创建图斑,保存到mongodb中的geomEntity表中，isCreate = true,生成metadata
    @GetMapping("/createGeom")
    public Map createGeom(@RequestParam("subtaskid") String subtaskid, @RequestParam("polygon") String polygon, @RequestParam("taskid") String taskid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.createGeomPG(subtaskid, polygon, taskid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //修改图斑,保存到mongodb中的geomEntity表中，isCreate = true,生成metadata
    @GetMapping("/updateGeom")
    public Map updateGeom(@RequestParam("subtaskid") String subtaskid, @RequestParam("edit_geoid") String edit_geoid, @RequestParam("polygon") String polygon) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.updateGeomPG(subtaskid, edit_geoid, polygon);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //合并geom
    @GetMapping("/unionGeom")
    public Map unionGeom(@RequestParam("edit_geoid") String edit_geoid, @RequestParam("polygon") String polygon) {
        Map returnMap = new HashMap();
        try {
            //mobileService.updateGeomPG(edit_geoid,polygon);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //删除图斑
    @GetMapping("/deleteGeomById")
    public Map deleteGeomById(@RequestParam("edit_geoid") String edit_geoid, @RequestParam("subtaskid") String subtaskid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.deleteGeomById(edit_geoid, subtaskid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //删除图斑和关联的表单、图片
    @GetMapping("/deleteGeomAndTableById")
    public Map deleteGeomAndTableById(@RequestParam("edit_geoid") String edit_geoid, @RequestParam("subtaskid") String subtaskid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.deleteGeomAndTableById(edit_geoid, subtaskid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //查询表单内容
    @GetMapping("/queryTables")
    public Map queryTables(@RequestParam("subtaskid") String subtaskid, @RequestParam("caseid") String caseid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.queryTables(subtaskid, caseid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    /**
     * @return
     * @Author zxw
     * @Description //TODO 上传视频
     * @Date 2023/10/24  22:45
     **/
    //提交表单,id为子任务的id,uploadFile为视频
    @PostMapping("/submitVideoFile")
    public Map submitVideoFile(@RequestParam("subtaskid") String subtaskid, @RequestParam("edit_geoid") String edit_geoid, @RequestParam("formcontentname") String formcontentname, @RequestParam("uploadFile") MultipartFile[] uploadFile
    ) {
        Map returnMap = new HashMap();
        try {
            String filename = mobileService.submitVideoFile(subtaskid, edit_geoid, formcontentname, uploadFile);
            returnMap.put("data", filename);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    /**
     * @return
     * @Author zxw
     * @Description //TODO 查询照片
     * @Date 2023/10/24  22:43
     **/
    //查询表单,id为子任务的id,uploadFile为图片
    @GetMapping("/queryTableFile")
    public Map queryTableFile(@RequestParam("subtaskid") String subtaskid, @RequestParam("edit_geoid") String edit_geoid) {
        Map returnMap = new HashMap();
        try {
            List list = mobileService.queryTableFile(subtaskid, edit_geoid);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    /**
     * @return
     * @Author zxw
     * @Description //TODO queryTableFileByCjy  采集员登录查询照片
     * @Date 2023/12/2  10:53
     **/
    //采集员登录查询照片  查询表单,id为子任务的id,uploadFile为图片 
    @GetMapping("/queryTableFileByCjy")
    public Map queryTableFileByCjy(@RequestParam("subtaskid") String subtaskid, @RequestParam("edit_geoid") String edit_geoid) {
        Map returnMap = new HashMap();
        try {
            List list = mobileService.queryTableFileByCjy(subtaskid, edit_geoid);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    /**
     * @return
     * @Author zxw
     * @Description //TODO 查询视频
     * @Date 2023/10/24  22:42
     **/
    //查询表单,id为子任务的id,uploadFile为视频 返回多个视频连接地址  本文视频文件夹不能删除
    @GetMapping("/queryVideoFile")
    public Map queryVideoFile(HttpServletResponse response, @RequestParam("subtaskid") String subtaskid, @RequestParam("edit_geoid") String edit_geoid) {
        Map<String, Object> returnMap = new HashMap<>();

        try {
            List<Map<String, String>> urlFilePaths = mobileService.queryVideoFile(subtaskid, edit_geoid);
            returnMap.put("data", urlFilePaths);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }

        return returnMap;
    }

    //删除图片,mongodb中的图片
    @GetMapping("/deleteTableFile")
    public Map deleteTableFile(@RequestParam("subtaskid") String subtaskid, @RequestParam("filename") String filename) {
        Map returnMap = new HashMap();
        try {
            mobileService.deleteTableFile(subtaskid, filename);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @ApiOperation("导出GPS的shp点和线数据")
    @PostMapping("/exportGPSShp")
    public Map exportGPSShp(HttpServletRequest request, HttpServletResponse response, @RequestParam("subtaskid") String subtaskid, @RequestParam("date_start") String date_start, @RequestParam("date_end") String date_end) {
        Map returnMap = new HashMap();
        try {

            mobileService.exportGPSShp(request, response, subtaskid, date_start, date_end);

            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    //添加gps轨迹
    @GetMapping("/addGPS")
    public Map addGPS(@RequestParam("nickname") String nickname, @RequestParam("create_time") String create_time, @RequestParam("userid") String userid, @RequestParam("username") String username, @RequestParam("taskid") String taskid, @RequestParam("taskname") String taskname, @RequestParam("subtaskid") String subtaskid, @RequestParam("lat") String lat, @RequestParam("lon") String lon) {
        Map returnMap = new HashMap();
        try {
            mobileService.addGPS(nickname, create_time, userid, username, taskid, taskname, subtaskid, lat, lon);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //获取时间间隔
    @GetMapping("/getTimeSpan")
    public Map getTimeSpan(@RequestParam("subtaskid") String subtaskid) {
        Map returnMap = new HashMap();
        try {
            Map map = mobileService.getTimeSpan(subtaskid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //获取位置信息
    @GetMapping("/getGPSByTime")
    public Map getGPSByTime(@RequestParam("subtaskid") String subtaskid, @RequestParam("minTime") String minTime, @RequestParam("maxTime") String maxTime) {
        Map returnMap = new HashMap();
        try {
            List list = mobileService.getGPSByTime(subtaskid, minTime, maxTime);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //同步数据，将mongodb中的数据同步到Pg里面
    @GetMapping("/syncData")
    public Map syncData(@RequestParam("taskid") String taskid, @RequestParam("caseid") String caseid) {
        Map returnMap = new HashMap();
        try {
            mobileService.syncData(taskid, caseid);

            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    //查询指定任务中所有已核查的图斑(globle_status字段为已核实)

    @GetMapping("/getCheckedPolygonByTaskId")
    public Map getCheckedPolygonByTaskId(@RequestParam("taskid") Integer taskid) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            List list = mobileService.getCheckedPolygonByTaskId(taskid);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    /**
     * @return
     * @Author zxw
     * @Description //TODO  按照编号查询已经完成的任务
     * @Date 2023/10/24  15:34
     **/
    @GetMapping("/queryById")
    public Map queryById(@RequestParam("id") String id, @RequestParam("taskid") String taskid) {
        Map returnMap = new HashMap();
        try {
            List list = mobileService.queryById(id, taskid);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }


    @ApiOperation("手绘范围——查询已完成任务")
    @PostMapping("/queryByDraw")
    public Result queryByDraw(@RequestBody String json) {
        return Result.success(mobileService.queryByDraw(json));
    }

    @ApiOperation("根据照片时间查询已经完成的任务")
    @GetMapping("/queryByPhotoTime")
    public Result queryByPhotoTime(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                   @RequestParam("taskid") String taskid) {

        return Result.success(mobileService.queryByTimeAndTaskId(startTime, endTime, taskid));
    }


    /**
     * @return
     * @Author zxw
     * @Description //删除视频 参考deleteTableFile接口
     * @Date 2023/10/24  15:34
     **/
    @GetMapping("/delVideoFileByIdFileName")
    public Map getCheckedPolygonByTaskId(@RequestParam("subtaskid") String subtaskid, @RequestParam("filename") String filename) {
        Map returnMap = new HashMap();
        try {
            mobileService.deleteVideoFile(subtaskid, filename);
            returnMap.put("data", "");
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @EnableKysLog("展示面板显示未完成任务的polygon——2024.04.08张所新提出需求")
    @ApiOperation("张所——展示面板显示未完成的polygon")
    @GetMapping("/queryUnCheckedByTaskId")
    public Result queryUnCheckedByTaskId(@RequestParam("taskid") Integer taskid) {
        List<SubtaskGeom> subtaskGeomList = mobileService.queryUnCheckedByTaskId(taskid);
        return Result.success(subtaskGeomList);
    }

}

